#ifndef __BOOTPARA_DEAL_H__
#define __BOOTPARA_DEAL_H__

#include <stdint.h>
#include <soc.h>
#include "board_partitions.h"

typedef struct {
    uint32_t magic; 
    uint32_t param;
    uint32_t res;
} boot_param_t;

typedef struct {
    boot_param_t bootmode_para;
    boot_param_t baudrate_para;
    boot_param_t app_state_para;
    boot_param_t boot_state_para;
    boot_param_t boot_part_para;
	boot_param_t powerOffState_part_para;
} boot_param_list_t;

typedef enum
{
    BOOT_PARA_NO_NEED_SET = 0,
    BOOT_PARA_NEED_SET = 1,
} boot_para_flag_t;

typedef struct {
    boot_para_flag_t flag;
    uint32_t para;
} boot_param_cfg_t;

typedef struct {
    boot_param_cfg_t bootmode_para_cfg;
    boot_param_cfg_t baudrate_para_cfg;
    boot_param_cfg_t app_state_para_cfg;
    boot_param_cfg_t boot_state_para_cfg;
    boot_param_cfg_t boot_part_para_cfg;
	boot_param_cfg_t powerOffState_part_para_cfg;
} boot_param_cfg_list_t;

typedef enum {
    FIRST_POWER_ON  = 0,     // first power on
    POWER_OFF_ON,            // abnormal power off        
    POWER_OFF_SLEEP,         // sleep 
    POWER_OFF_MAX_NUM_STATES,
} BOOTPARAM_POWER_OFF_PRE_STATA;

#define SUCCESS                         0
#define FAILURE                         -1

#define MAGICNUM                        0x4e5a5751                                                          /* QWZN */
#define BOOTPARAM_MAGICNUM              MAGICNUM                                                            /* QWZN */
#define BOOTMODE_PARAM_ADDR             BOOTLOADER_PARAM_PARTITION_START
#define BAUDRATE_PARAM_ADDR             (BOOTMODE_PARAM_ADDR + sizeof(boot_param_t))
#define APP_STATE_PARAM_ADDR            (BAUDRATE_PARAM_ADDR + sizeof(boot_param_t))
#define BOOT_STATE_PARAM_ADDR           (APP_STATE_PARAM_ADDR + sizeof(boot_param_t))
#define BOOT_PART_PARAM_ADDR            (BOOT_STATE_PARAM_ADDR + sizeof(boot_param_t))
#define POWER_OFF_PRESTATE_PARAM_ADDR   (BOOT_PART_PARAM_ADDR + sizeof(boot_param_t))


#define BOOTPARAM_DEFAULT_BAUDRATE      115200
#define BOOTPARAM_MAX_BAUDRATE          (5 * 1024 * 1024)                                                   /* 5Mbps */
#define BOOTPARAM_MIN_BAUDRATE          0                                              

#define DOWN_MODE                       0x4E574F44                                                          /* "DOWN" mode*/
#define BOOT_MODE                       0x544F4F42                                                          /* "BOOT" mode */
#define BOOT_MODE_NUM                   1
#define DOWN_MODE_NUM                   2
#define BOOTPARAM_DEFAULT_BOOTMODE      DOWN_MODE

#define APP_VALID                       0x494C4156                                                          /* APP "VALID" */                   
#define APP_INVALID                     0x41564E49                                                          /* APP "INVALID" */                   
#define APP_VALID_NUM                   1
#define APP_INVALID_NUM                 2
#define BOOTPARAM_DEFAULT_APP_STATE     APP_INVALID                                                        

#define FIRST_BOOT                       0x53524946                                                          /* "FIRST" boot */                   
#define NON_FIRST_BOOT                   0x5249464E                                                          /* "NON-FIRST" boot */                   
#define FIRST_BOOT_NUM                   1
#define NON_FIRST_BOOT_NUM               2
#define BOOTPARAM_DEFAULT_BOOT_STATE     FIRST_BOOT                                                        

#define APP_PART                         0x42505041                                                          /* "APPB" boot */                   
#define OTA_PART                         0x4241544F                                                          /* "OTAB" boot */                   
#define APP_PART_NUM                     1
#define OTA_PART_NUM                     2
#define BOOTPARAM_DEFAULT_BOOT_PART      APP_PART                                                        


#define BOOTPARAM_DEFAULT_POWER_OFF_PRE_STATA    FIRST_POWER_ON

int first_time_bootup(void);
uint32_t get_bootmode(void);
uint32_t get_baudrate(void);
uint32_t get_app_state(void);
uint32_t get_boot_state(void);
uint32_t get_boot_part(void);
uint32_t get_boot_part_index(void);
uint32_t get_update_part_index(void);
int set_baudrate(uint32_t baudrate);
int set_bootmode(uint8_t bootmode);
int set_app_state(uint8_t app_state);
int set_boot_state(uint8_t boot_state);
int set_boot_part(uint8_t boot_part);
int set_bootpara_list(boot_param_cfg_list_t *boot_param_cfg_list);
int setup_bootpara_afterburn(uint32_t boot_base);

#endif
